package com.openrange.dukes.Dukes
{
	import com.openrange.dukes.GUI;
	import com.openrange.dukes.Input.BaseInput;
	import com.openrange.dukes.PlayState;
	import com.openrange.dukes.Terrain;
	import flash.geom.Point;
	import org.flixel.*;
	
	public class Duke extends FlxSprite
	{
		[Embed(source = "../../../../../assets/player.png")] static public var ImgPawn:Class;
		
		private var startPosition:Point;
		private var targetPosition:Point;
		private var tilePosition:Point;
		private var state:PlayState;
		private var map:Terrain;
		private var gui:GUI;
		
		private var inputClass:BaseInput;
		
		//can only be moved if it is the duke's turn
		private var activeDuke:Boolean = false;
		public var movementAllow:int = 0;
			
		public function Duke(tilePos:Point, input:BaseInput, map:Terrain, gui:GUI)
		{
			super(map.getScreenPositionX(tilePos.x), map.getScreenPositionY(tilePos.y));
			
			startPosition = new Point(tilePos.x, tilePos.y);
			tilePosition = new Point(tilePos.x, tilePos.y);
			targetPosition = new Point(tilePos.x, tilePos.y);
			
			inputClass = input;
			this.map = map;		
			this.gui = gui;
			
			loadGraphic(ImgPawn, false, false);
			
			state = FlxG.state as PlayState;
			
			SetUpGUI();
		}
		
		
		public function activate():void
		{
			activeDuke = true;
			movementAllow = 2;
			SetUpGUI();
		}
		public function deactivate():void
		{
			activeDuke = false;
			movementAllow = 0;
		}
		
		public function Update():void
		{
			if (activeDuke)
			{
				inputClass.getInput(this);
			}
		}
		
		public function move(tilesX:int, tilesY:int):void
		{
			if (movementAllow > 0)
			{
				if (map.isTilePassable(tilePosition.x + tilesX, tilePosition.y + tilesY))
				{
					x += map.tileWidth * tilesX;
					y += map.tileHeight*tilesY;
					tilePosition.y += tilesY;
					tilePosition.x += tilesX;
					movementAllow--;
					SetUpGUI();
				}
			}
		}
		public function SetUpGUI():void
		{
			gui.SetTerrain(map.getTileType(tilePosition.x, tilePosition.y));
		}
	}

}